
var editor; // use a global for the submit and return data rendering
var table;
var userTypeId = 0;
var user;
var communityId="";
function getUrl(url,params) {
    for (var i=0;i<params.length;i++){
        if ($("#"+params[i]).val()!= "") {
            url = url + "&"+params[i]+"=" + $("#"+params[i]).val();
        }
    }
    return url;

}
$(document).ready(function () {
    $.ajax({
        url: '/commis/api/user/currentUser',
        type: "GET",
        async: false,
        dataType: 'json',
        data: {},
        success: function (response) {
            if (response.status == 0) {
                console.log("用户存在");
                user = response.data;
                userTypeId = user.userTypeId;
                if(userTypeId==4){
                    communityId=user.communityId;
                }
                console.log(userTypeId);
            } else if (response.status == 1) {
                alert("登录用户已经过期，请重新登录");
                window.parent.location.href = "../../login.html";
            }
        }, error: function (msg) {
            alert("登录用户已经过期，请重新登录");
            window.parent.location.href = "../../login.html";
        }
    })
    $.ajax({
        url: '/commis/api/community/allCom',
        async: false,
        dataType: 'json',
        success: function (response) {
            console.log(response.length);
            for (var i = 0; i < response.length; i++) {
                $("#community").append("<option value='" + response[i].communityId + "'>" + response[i].communityName + "</option>");
            }


        }, error: function () {

        }
    });

    var fields= [
      /*  {
            label: "基本工资:",
            name: "basicSalary"
        }, */{
            label: "起点年限补贴",
            name: "qdnxSub"
        }, {
            label: "独生子女补贴",
            name: "dsznSub"
        }, {
            label: "岗位工资",
            name: "postSalary"
        }, {
            label: "届期津贴",
            name: "espSub"
        }
        , {
            label: "专业技术等级工资",
            name: "tecSalary"
        }
        , {
            label: "工作津贴",
            name: "workSub"
        }
        , {
            label: "生活补贴",
            name: "existSub"
        }
        , {
            label: "月奖金",
            name: "monthBonus"
        }
        , {
            label: "养老",
            name: "pension"
        }
        , {
            label: "医疗",
            name: "medicare"
        }
        , {
            label: "失业",
            name: "unemployment"
        } , {
            label: "公积金",
            name: "providentFund"
        } , {
            label: "个人所得税",
            name: "incomeTax"
        } , {
            label: "补扣公积金",
            name: "supPf"
        } , {
            label: "补扣三险",
            name: "supI"
        }
    ]

    editor = new $.fn.dataTable.Editor({
        ajax: {
            create: {
                type: 'POST',
                url: '/commis/api/sWage/create'
            },
            edit: {
                type: 'PUT',
                url: '/commis/api/sWage/edit?id=_id_'
            },
            remove: {
                type: 'DELETE',
                url: '/commis/api/sWage/remove?id=_id_'
            }
        },
        i18n: {
            create: {
                title: "新增记录",
                submit: "添加"
            },
            edit: {
                title: "修改记录",
                submit: "修改",
                confirm: "您确定要修改这 %d 条记录吗?"
            },
            remove: {
                title: "删除记录",
                submit: "删除",
                confirm: "您确定要删除这 %d 条记录吗?"
            }
        },
        idSrc: 'swageId',
        table: "#dataTable",
        fields:fields
    });
    var buttons = [];
    var select = true;
    if (userTypeId == 1) {
        $("#communityDiv").show();
        select = true;
        buttons = [
           /* {extend: "edit", editor: editor, text: "修改", className: 'btn btn-primary'}*/
        ];



    } else if (userTypeId == 2) {
        $("#communityDiv").show();
        select = false;
        buttons = [];

    } else if (userTypeId == 3||userTypeId == 6||userTypeId == 7||userTypeId == 8||userTypeId == 9) {
        $("#communityDiv").show();
        select = false;
        buttons = [];

    } else if (userTypeId == 4) {
        $("#communityDiv").hide();
        select = false;
        buttons = [];

    }

     table =$('#dataTable').DataTable({
         dom: "Bfrtip",
        ajax: "/commis/api/fWage/pageDatatables",
         autoWidth:true,
         responsive: true,
        serverSide: true,
        processing: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        searching: false,//是否开始本地搜索
        language: {
            emptyTable: "没有数据",
            search: "搜索:",
            paginate: {
                first: "首页",
                previous: "上一页",
                next: "下一页",
                last: "尾页"

            },
            lengthMenu: "每页 _MENU_ 条记录",
            zeroRecords: "没有找到记录",
            info: "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )，共 _TOTAL_ 项",
            infoEmpty: "无记录",
            infoFiltered: "(从 _MAX_ 条记录过滤)"
        },
        columns: [
            {
                data: 'community.communityName', defaultContent: ""
            }, {
                data: 'depart.departName', defaultContent: ""
            }, {
                data: 'user.realName', defaultContent: ""
            },{
                data: 'personName', defaultContent: ""
            }, {
                data: 'postSalary', defaultContent: ""
            }, {
                data: 'espSub', defaultContent: ""
            }, {
                data: 'tecSalary', defaultContent: ""
            }, {
                data: 'workSub', defaultContent: ""
            }, {
                data: 'existSub', defaultContent: ""
            }, {
                data: 'monthBonus', defaultContent: ""
            }, {
                data: 'perBonus', defaultContent: ""
            }, {
                data: 'backPay', defaultContent: ""
            }, {
                data: 'leaveDed', defaultContent: ""
            }, {
                data: 'sumWage', defaultContent: ""
            }, {
                data: 'wageMonth', defaultContent: ""
            }, {
                data: 'wageYear', defaultContent: ""
            }, {
                data: 'registerTime', defaultContent: ""
            }],
         select: select,
         buttons:  buttons

    });








})
$('#calculate').on('click', function () {


    $.ajax({
        url: '/commis/api/sWage/calculate',
        async: false,
        dataType: 'json',
        success: function (response) {
            if(response.status==0){
                alert("当月工资计算成功");
                table.ajax.reload();
            }else{
                alert(response.msg);
            };



        }, error: function () {
            alert("发生错误");
        }
    });



});
$('#search').on('click', function () {

    var url = "/commis/api/fWage/pageDatatables?1=1"
    var communityId = $('#community').val();
    url = url + "&communityId=" + communityId;
    var year = $('#year').val();
    if (year != "") {
        if(isNaN(year)){
            toastr["warning"]("查询年份必须是4位数字");
            return false;
        }else{
            if(year.length!=4){
                toastr["warning"]("查询年份必须是4位数字");
                return false;

            }
        }
        url = url + "&wageYear=" + year;
    }
    if ($("#month").val() != "") {
        var month = $('#month').val();
        url = url + "&wageMonth=" + month;
    }


    table.ajax.url(url).load();
});
//设置框架
$(function () {
    $("#page_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});